package com.faithbj.controller.statistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;

import com.faithbj.common.model.Region;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.common.utils.SystemConfigUtil;
import com.faithbj.common.web.controller.BaseController;
import com.faithbj.service.RegionService;

/**
 * 统计分析的controller基类
 * 
 * @author m
 * 
 */
public class StatBaseController extends BaseController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2893596596674889529L;

	@Resource
	private RegionService regionService;

	@Resource
	private SystemConfigUtil systemConfigUtil;

	// 默认的区县列表值
	protected static List<Region> countryRegionList;

	protected void initCountryRegionList() {
		StatBaseController.countryRegionList = this.getAllCountryRegion();
	}

	/**
	 * 获取所有的区县列表
	 */
	private List<Region> getAllCountryRegion() {
		return regionService.getRegionListByType(Region.COUNTRY);
	}

	/**
	 * 计算条件中的起始日期
	 * 
	 * @param now
	 * @return
	 */
	public Date getStartTime(Date endDate) {
		int diff = Integer.parseInt(systemConfigUtil.getDiffDays());
		return DateUtils.dateAddDays(endDate, -diff);
	}

	/**
	 * 根据用户输入的区县与街道，得到一个符合条件的区县列表
	 * 
	 * @param countiesRegionId
	 * @param streetRegionId
	 * @return
	 */
	public List<Region> getCountryRegionList(String countiesRegionId,
			String streetRegionId) {
		List<Region> countryRegionList = new ArrayList<Region>();
		if (StringUtils.isBlank(countiesRegionId)) {
			countryRegionList = this.getAllCountryRegion();
		} else {
			if (StringUtils.isBlank(streetRegionId)) {
				countryRegionList.add(regionService
						.getRegionById(countiesRegionId));
			} else {
				Region countryRegion = regionService
						.getRegionById(countiesRegionId);
				Region streetRegion = regionService
						.getRegionById(streetRegionId);
				Set<Region> childSet = new HashSet<Region>();
				childSet.add(streetRegion);
				countryRegion.setChildren(childSet);
				countryRegionList.add(countryRegion);
			}
		}
		return countryRegionList;
	}

}
